using System.Collections;
using Concurso;
using Concurso.Clases;
using FunkeLab;

namespace ConcursoCV.UI.WebUserControls.AdmVersionesCV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ControladorVersionCV.
	/// </summary>
	public class ControladorVersionCV : System.Web.UI.UserControl
	{
		protected WebUserControls.AdmVersionesCV.ArbolVersionCV ucArbolVersionCV;
		protected WebUserControls.AdmVersionesCV.ABMAntecedenteVersionCV ucABMAntecedenteVersionCV;
		protected WebUserControls.AdmVersionesCV.AyudaVersionCV ucAyudaVersionCV;


		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				this.MostrarAyuda();
			}
		}

		private void MostrarAyuda()
		{
			MostrarControles(false,true);
		}
		private void MostrarDetalle()
		{
			MostrarControles(true,false);
		}
		private void MostrarControles( bool bDetalle, bool bAyuda )
		{
			ucAyudaVersionCV.Visible = bAyuda;
			ucABMAntecedenteVersionCV.Visible = bDetalle;			
		}

		private void HabilitarDeshabilitarAgregarYModificar(bool enabled)
		{
			this.ucArbolVersionCV.AgregarEnabled = enabled;
			this.ucArbolVersionCV.ModificarEnabled = enabled;
		}

		#region - properties -

		public string SelectedNode 
		{
			get{ return  (string) ViewState["_selectedNode"];}	
			set{ ViewState["_selectedNode"] = value;}
		}

		public ArrayList SelectedPathNode 
		{
			get{ return (ArrayList) ViewState["_pathNode"];}	
			set{ ViewState["_pathNode"] = value;}
		}

		public string SelectedPath 
		{
			get{ return (string) ViewState["_selectedPath"];}	
			set{ ViewState["_selectedPath"] = value;}
		}

		

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			MyInitializeComponent();
			base.OnInit(e);
		}
		
		private void MyInitializeComponent()
		{
			// Arbol 
			this.ucArbolVersionCV.Add+=new EventHandler(ucArbolVersionCV_Add);
			this.ucArbolVersionCV.Modificar +=new EventHandler(ucArbolVersionCV_Modificar);
			//
			this.ucArbolVersionCV.TreeViewNodeClick +=new EventHandler(ucArbolVersionCV_TreeViewNodeClick);
			this.ucArbolVersionCV.TreeViewNodeOnDemand +=new EventHandler(ucArbolVersionCV_TreeViewNodeOnDemand);
			this.ucArbolVersionCV.TreeViewOnNodeClose +=new EventHandler(ucArbolVersionCV_TreeViewOnNodeClose);
			// ABM AntecedenteMetadato
			this.ucABMAntecedenteVersionCV.Add +=new EventHandler(ucABMAntecedenteVersionCV_Add);
			this.ucABMAntecedenteVersionCV.Modificar+=new EventHandler(ucABMAntecedenteVersionCV_Modificar);
			this.ucABMAntecedenteVersionCV.Cancelar +=new EventHandler(ucABMAntecedenteVersionCV_Cancelar);
		}
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ucArbolVersionCV_TreeViewNodeClick(object sender, EventArgs e)
		{
			MyTreeViewItem item = ((TreeViewEventArgs) e).Node;
			this.SelectedNode = item.Value;
			if(item != null)
			{
				if(item.Value == "ROOT")
				{
					MostrarAyuda();
				}
				else
				{
					ucABMAntecedenteVersionCV.PageMode = PageMode.EDIT;
					ucABMAntecedenteVersionCV.listaAtributosViewState = null;
					ucABMAntecedenteVersionCV.CargarAntecedenteAControl(item.Value);
					ucABMAntecedenteVersionCV.EnabledDisableControls( false );
					MostrarDetalle();					
				}
			}
		}

		private void ucArbolVersionCV_TreeViewNodeOnDemand(object sender, EventArgs e)
		{

		}

		private void ucArbolVersionCV_TreeViewOnNodeClose(object sender, EventArgs e)
		{
			if( ucArbolVersionCV.SelectedNode != null) 
			{							
				if ( !ucArbolVersionCV.IsSelectedNode( ucArbolVersionCV.SelectedPathNode ) ) 
				{					
					SelectedNode = null;
					SelectedPathNode = null;
					HabilitarDeshabilitarAgregarYModificar(false);
				}				
			}			
			else //se cerro el nodo raiz
			{
				SelectedPathNode = null;
				HabilitarDeshabilitarAgregarYModificar(false);
			}
		}

		private void ucArbolVersionCV_Add(object sender, EventArgs e)
		{
			HabilitarDeshabilitarAgregarYModificar(false);
			this.ucABMAntecedenteVersionCV.PageMode = PageMode.NEW;
			this.ucABMAntecedenteVersionCV.AntecedenteMetadatoPadreId = this.ucArbolVersionCV.SelectedNode.ToString() != "ROOT" ? this.ucArbolVersionCV.SelectedNode.ToString() : null;
			this.ucABMAntecedenteVersionCV.Nuevo();
			MostrarDetalle();
		}

		private void ucABMAntecedenteVersionCV_Add(object sender, EventArgs e)
		{
			this.ucArbolVersionCV.RefreshTreeView();
			if(this.ucABMAntecedenteVersionCV.PageMode == PageMode.NEW)
			{
				// actualizo el SaveSelectedNode
				this.ucArbolVersionCV.SelectedNode = this.ucABMAntecedenteVersionCV.AntecedenteMetadatoId;
				// Selecciono el nodo
				this.ucArbolVersionCV.SelectNode(
					this.ucArbolVersionCV.SelectedPathNode, 
					 this.ucABMAntecedenteVersionCV.AntecedenteMetadatoId
					);
			}
			else
			{
				this.ucArbolVersionCV.SelectedNode = this.ucABMAntecedenteVersionCV.AntecedenteMetadatoId;
				// Selecciono el nodo
				this.ucArbolVersionCV.SelectNode(this.SelectedPathNode);				
			}
			HabilitarDeshabilitarAgregarYModificar(true);
		}

		private void ucArbolVersionCV_Modificar(object sender, EventArgs e)
		{
			HabilitarDeshabilitarAgregarYModificar(false);
			this.ucABMAntecedenteVersionCV.PageMode = PageMode.EDIT;
			this.ucABMAntecedenteVersionCV.Modificacion();
			MostrarDetalle();
		}

		private void ucABMAntecedenteVersionCV_Modificar(object sender, EventArgs e)
		{
			this.ucArbolVersionCV.RefreshTreeView();
			HabilitarDeshabilitarAgregarYModificar(true);
		}

		private void ucABMAntecedenteVersionCV_Cancelar(object sender, EventArgs e)
		{
			if(this.SelectedNode != null)
			{
				if(this.SelectedNode == "ROOT")
				{
					MostrarAyuda();
				}
				else
				{
					ucABMAntecedenteVersionCV.PageMode = PageMode.EDIT;
					ucABMAntecedenteVersionCV.CargarAntecedenteAControl(this.SelectedNode);
					ucABMAntecedenteVersionCV.EnabledDisableControls( false );
					MostrarDetalle();					
				}
			}			
			HabilitarDeshabilitarAgregarYModificar(true);
		}
	}
}
